package com.xlp;



import com.entity.Sendsms;
import com.entity.User;
import com.jch.Dao.SendsmsDao;
import com.jch.Dao.UserDao;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import sun.awt.WindowClosingListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;

public class RegisterFrame extends JDialog{
	private static final long serialVersionUID = 1L;
	private JPanel JP;
	private JRadioButton ComUserJRB;//普通
	private JRadioButton RootJRB;//管理员管理
	private ButtonGroup BG;//
	private JLabel NameJL;//昵称
	private JTextField NameJTF;//昵称填写框
	private JLabel PasswordJL;//密码
	private JPasswordField PasswordJPF;//密码框
	private JLabel AffirmPasswordJL;//确认密码
	private JPasswordField AffirmPasswordJPF;//确认密码框
	private JLabel VerificationCodeJL;//验证码
	private JTextField VerificationCodeJTF;//验证码框
	private JButton GetCaptcha;//获取验证码按钮
	private JButton RegisterJB;//注册按钮
	private JRadioButton isbutton; //判断选择的按钮是哪个

	private JLabel PhoneNumJL;
	private JTextField PhoneNumJTF;

	private int i;
	private RegisterFrame registerFrame;

	private JTextArea jta_Name;
	private JTextArea jta_Password;
	private JTextArea jta_surePassword;
	private JTextArea jta_phone;

	public static String smsCode() {

		String random = (int) ((Math.random() * 9 + 1) * 100000) + "";

		return random;

	}
	// 获取时间戳

	public static String getTimestamp() {

		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

	}

	public RegisterFrame() {
		ImageIcon icon=new ImageIcon(RegisterFrame.class.getResource("picture.png"));


		setTitle("用户注册");
		//主面板
		JP=new JPanel() {
			private static final long serialVersionUID = 1L;
			@Override
			protected void paintComponent(Graphics g) {

				g.drawImage(icon.getImage(), 0, 0, this);
			}
		};
		JP.setLayout(null);
		ComUserJRB=new JRadioButton("普通用户注册",true);
		ComUserJRB.setForeground(Color.WHITE);
		ComUserJRB.setContentAreaFilled(false);
		ComUserJRB.setBounds(4, 4, 110, 30);
		ComUserJRB.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange()==ItemEvent.SELECTED){
					i=0;
				}
			}
		});

		JP.add(ComUserJRB);
		RootJRB=new JRadioButton("管理员管理");
		RootJRB.setOpaque(false);
		RootJRB.setForeground(Color.WHITE);
		RootJRB.setBounds(114, 4, 100, 30);
		RootJRB.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange()==ItemEvent.SELECTED){
					i=1;
				}
			}
		});
		JP.add(RootJRB);
		BG=new ButtonGroup();
		BG.add(ComUserJRB);
		BG.add(RootJRB);
		//昵称
		NameJL =new JLabel("昵称：");
		NameJL.setFont(new Font("宋体", Font.PLAIN, 20));
		NameJL.setForeground(Color.WHITE);
		NameJL.setBounds(330, 101, 300, 40);
		JP.add(NameJL);
		//提示用户名文本域
		jta_Name=new JTextArea();
		jta_Name.setBounds(430,130,380,40);
		jta_Name.setEnabled(false);
		jta_Name.setOpaque(false);
		JP.add(jta_Name);

		NameJTF=new JTextField();
		NameJTF.setFont(new Font("宋体", Font.PLAIN, 35));
		NameJTF.setBounds(420, 101, 340, 40);
		NameJTF.setBorder(null);
		NameJTF.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				NameJTF.setText("");
				jta_Name.setFont(new Font("微软雅黑",Font.PLAIN,14));
				jta_Name.setForeground(Color.WHITE);
				jta_Name.setText("\n"+"4-16个字符，可使用字母、数字、下划线,中文。");
			}
		});
		NameJTF.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				boolean	email = Pattern.matches("^[\\u4E00-\\u9FA5A-Za-z0-9_]{3,16}$",NameJTF.getText() );

				if(email==false) {

					    if("".equals(NameJTF.getText()) ) {

							jta_Name.setText("");
							jta_Name.setEnabled(false);
						}
					    else {
							jta_Name.setEnabled(true);
							NameJTF.getText();
							NameJTF.setBorder(BorderFactory.createLineBorder(Color.CYAN));
							jta_Name.setFont(new Font("微软雅黑", Font.PLAIN, 14));
							jta_Name.setForeground(Color.CYAN);
							jta_Name.setText("\n" + "  格式不正确！");
						}


				}else if(email==true){
					jta_Name.setEnabled(true);
					NameJTF.getText();
					NameJTF.setBorder(BorderFactory.createLineBorder(Color.GRAY));//边框颜色还原
					jta_Name.setFont(new Font("微软雅黑",Font.PLAIN,14));
					jta_Name.setForeground(Color.GREEN);
					jta_Name.setText("\n"+"  恭喜，该用户名可以注册");
				}


			}
			@Override
			public void focusGained(FocusEvent e) {
				NameJTF.setBorder(BorderFactory.createLineBorder(Color.GRAY));
				jta_Name.setText("\n"+"4-16个字符，可使用字母、数字、下划线,中文。");
			}


		});

		JP.add(NameJTF);

		//密码
		PasswordJL =new JLabel("设置密码：");
		PasswordJL.setFont(new Font("宋体", Font.PLAIN, 20));
		PasswordJL.setForeground(Color.WHITE);
		PasswordJL.setBounds(330, 181, 300, 40);
		JP.add(PasswordJL);
		//提示密码文本域
		jta_Password=new JTextArea();
		jta_Password.setBounds(420,210,340,40);
		jta_Password.setEnabled(false);
		jta_Password.setOpaque(false);
		JP.add(jta_Password);

		PasswordJPF=new JPasswordField();
		PasswordJPF.setFont(new Font("宋体", Font.PLAIN, 40));
		PasswordJPF.setBounds(420, 181, 340, 40);
		PasswordJPF.setBorder(null);
		PasswordJPF.setForeground(Color.GRAY);
		PasswordJPF.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				PasswordJPF.setText("");
				jta_Password.setFont(new Font("微软雅黑",Font.PLAIN,14));
				jta_Password.setForeground(Color.GRAY);
				//PasswordJPF.setBorder(BorderFactory.createLineBorder(Color.BLUE));
				jta_Password.setText("\n"+"6-16个字符，区分大小写");
			}
		});
		PasswordJPF.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				boolean psw=Pattern.matches("^[A-za-z0-9]{6,16}$",PasswordJPF.getText());
				if(psw==false) {
					if("".equals(PasswordJPF.getText()) ) {
						jta_Password.setText("");
						jta_Password.setEnabled(false);
					}else {
						jta_Password.setEnabled(true);
						PasswordJPF.getText();

						jta_Password.setFont(new Font("微软雅黑", Font.PLAIN, 14));
						jta_Password.setForeground(Color.GRAY);
						jta_Password.setText("\n" + "  密码错误！");
					}
				}else if(psw==true){
					jta_Password.setEnabled(true);
					PasswordJPF.getText();
					PasswordJPF.setBorder(BorderFactory.createLineBorder(Color.GRAY));//边框颜色还原
					jta_Password.setFont(new Font("微软雅黑",Font.PLAIN,14));
					jta_Password.setForeground(Color.GREEN);
					jta_Password.setText("\n"+"  恭喜，密码可以使用");
				}
			}
			@Override
			public void focusGained(FocusEvent e) {

				jta_Password.setText("\n"+"6-16个字符，区分大小写");
				PasswordJPF.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			}


		});
		JP.add(PasswordJPF);

		//确认密码框
		AffirmPasswordJL=new JLabel("确认密码：");
		AffirmPasswordJL.setForeground(Color.WHITE);
		AffirmPasswordJL.setFont(new Font("宋体", Font.PLAIN, 20));
		AffirmPasswordJL.setBounds(330, 261, 300, 40);
		JP.add(AffirmPasswordJL);
		//确认密码提示文本域
		jta_surePassword=new JTextArea();
		jta_surePassword.setBounds(420,310,380,40);
		jta_surePassword.setEnabled(false);
		jta_surePassword.setOpaque(false);
		JP.add(jta_surePassword);

		AffirmPasswordJPF=new JPasswordField();
		AffirmPasswordJPF.setBounds(420, 261, 340, 40);
		AffirmPasswordJPF.setFont(new Font("宋体", Font.PLAIN, 40));
		AffirmPasswordJPF.setBorder(null);
		AffirmPasswordJPF.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				AffirmPasswordJPF.setText("");
				jta_surePassword.setText("");
				jta_surePassword.setEnabled(false);
				//AffirmPasswordJPF.setBorder(BorderFactory.createLineBorder(Color.BLUE));
				if("".equals(jta_Password.getText())) {
					jta_surePassword.setText("请先输入密码");
				}
			}
		});

		AffirmPasswordJPF.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent e) {

			}

			@Override
			public void focusLost(FocusEvent e) {
				if (PasswordJPF.getText()!="" ){
					if("".equals(AffirmPasswordJPF.getText())){
						jta_surePassword.setText("");
					}else if(!PasswordJPF.getText().equals(AffirmPasswordJPF.getText())){
						jta_surePassword.setText("两次密码不一致");
					}else if(PasswordJPF.getText().equals(AffirmPasswordJPF.getText())){
						jta_surePassword.setText(" OK");
						jta_surePassword.setEnabled(true);
						jta_surePassword.setForeground(Color.GREEN);
					}else{
						jta_surePassword.setText(" 输入错误！");
					}
				}

			}
		});
		JP.add(AffirmPasswordJPF);
		//手机号
		PhoneNumJL =new JLabel("手机号码:");
		PhoneNumJL.setFont(new Font("宋体", Font.PLAIN, 20));
		PhoneNumJL.setForeground(Color.WHITE);
		PhoneNumJL.setBounds(330, 341, 300, 40);
		JP.add(PhoneNumJL);
		PhoneNumJTF=new JTextField();
		PhoneNumJTF.setBorder(null);
		PhoneNumJTF.setBounds(420, 341, 340, 40);
		PhoneNumJTF.setFont(new Font("宋体", Font.PLAIN, 40));
		JP.add(PhoneNumJTF);
		//验证码
		VerificationCodeJL=new JLabel("请填写手机验证码：");
		VerificationCodeJL.setFont(new Font("宋体", Font.PLAIN, 20));
		VerificationCodeJL.setForeground(Color.WHITE);
		VerificationCodeJL.setBounds(305, 421, 300, 40);
		JP.add(VerificationCodeJL);
		VerificationCodeJTF=new JTextField();
		VerificationCodeJTF.setBounds(480, 421, 155, 40);
		VerificationCodeJTF.setBorder(null);
		VerificationCodeJTF.setFont(new Font("宋体", Font.PLAIN, 40));
		JP.add(VerificationCodeJTF);
		GetCaptcha=new JButton("获取验证码");
		GetCaptcha.setFont(new Font("宋体", Font.PLAIN, 20));
		GetCaptcha.setBounds(635, 421, 134, 40);
		GetCaptcha.setFocusPainted(false);
		GetCaptcha.setContentAreaFilled(false);
		GetCaptcha.addActionListener(new ActionListener() {
			String random = (int) ((Math.random() * 9 + 1) * 100000) + "";
			@Override
			public void actionPerformed(ActionEvent e) {
				new SendSms(PhoneNumJTF.getText(),random);
				SendsmsDao sendsmsDao=new SendsmsDao();

					sendsmsDao.InsertValues(PhoneNumJTF.getText(), random);

				Timer timer=new Timer();
				timer.schedule(new TimerTask() {
					@Override
					public void run() {
						sendsmsDao.deleteSendsms(random);
					}
				},300000,1);
				timer.cancel();
			}
		});

		JP.add(GetCaptcha);
		//注册按钮
		RegisterJB=new JButton("立即注册");
		//RegisterJB.setBorder(null);
		RegisterJB.setFocusPainted(false);
		RegisterJB.setContentAreaFilled(false);
		RegisterJB.setFont(new Font("幼圆", Font.PLAIN, 30));
		//RegisterJB.setForeground(Color.WHITE);
		RegisterJB.setBounds(495, 470, 154, 40);
		RegisterJB.addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseReleased(MouseEvent e) {
//				// TODO Auto-generated method stub
//				RegisterJB.setOpaque(false);
//			}
//
//			@Override
//			public void mousePressed(MouseEvent e) {
//				// TODO Auto-generated method stub
//				RegisterJB.setOpaque(true);
//				RegisterJB.setBackground(new Color(249,244,250));
//
//			}
//			@Override
			//点击事件
			public void mouseClicked(MouseEvent e) {
				User user = new User();
				UserDao userDao = new UserDao();
				//调用方法查询数据库中是否与昵称文本域中内容相同
				List<User> users = userDao.queryUsersByuName(NameJTF.getText());
				for (User user1 : users) {
					user.setuName(user1.getuName());
				}

				//如果数据库中无这个昵称就设置User类中的昵称
				if(user.getuName()==null){
					user.setuName("");
				}
				if ("".equals(NameJTF.getText())){
					JOptionPane.showMessageDialog(registerFrame, "请输入用户名");
					return;
				}else if ("".equals(PasswordJPF.getText())){
					JOptionPane.showMessageDialog(registerFrame, "请输入密码");
					return;
				}else if("".equals(PhoneNumJTF.getText())){
					JOptionPane.showMessageDialog(registerFrame, "请输入手机号");
					return;
				}
                //如果数据库中的昵称和文本域相同,数据添加失败
				if(user.getuName().equals(NameJTF.getText())){
					JOptionPane.showMessageDialog(registerFrame, "该用户名已被注册");
					return;
					//数据库中的昵称和文本域不相同
				}else if(!user.getuName().equals(NameJTF.getText()) ){
					//执行添加的sql，将昵称、密码、型号存储到数据库
					int userss = userDao.InsertValues(NameJTF.getText(), PasswordJPF.getText(), i,PhoneNumJTF.getText());
					user.setuName(NameJTF.getText());
					user.setuPass(PasswordJPF.getText());
					user.setuType(i);
					user.setUphone(PhoneNumJTF.getText());

				}
				if (GetCaptcha.getText()==null&&GetCaptcha.getText()!=new SendsmsDao().querySendsms(PhoneNumJTF.getText()).getRandom()){
					JOptionPane.showMessageDialog(registerFrame,"验证码无效");
				}

				if (NameJTF.getText().equals(user.getuName()) &&
						PasswordJPF.getText().equals(user.getuPass())
						&& PasswordJPF.getText().equals(AffirmPasswordJPF.getText())
				    && PasswordJPF.getText()!=null && PhoneNumJL.getText()!=null) {
					System.out.println(PasswordJPF.getText());
					JOptionPane.showMessageDialog(registerFrame, "注册成功");
					dispose();
				} else {
					JOptionPane.showMessageDialog(registerFrame, "注册失败");
				}
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				setCursor(new Cursor(12));
			}
			@Override
			public void mouseExited(MouseEvent e) {
				setCursor(new Cursor(0));
			}
		});
		JP.add(RegisterJB);
		add(JP);

		setSize(icon.getIconWidth()-50,icon.getIconHeight()-100);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		setResizable(false);
		setVisible(true);
	}
	public static void main(String[] args) {
		new RegisterFrame();
	}
}
